package fan.project1_SpeedTalkSystem.Interceptor;

import fan.project1_SpeedTalkSystem.Model.JWTUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        String userToken = request.getHeader("userToken");
        //验证token是否有效
        log.info("userToken:"+userToken);
        Claims msg = JWTUtils.getMsg(userToken);

        if(msg==null){
            log.error("没有获取到token");
            response.setStatus(401);
            return false;
        }
        System.out.println("当前用户："+msg.getOrDefault("userName", "未获取到用户名"));
        log.info("用户token验证通过");
        return true;
    }
}
